﻿<UserControl x:Class="OI.Views.MashupLegendView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"  
    xmlns:Micro="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro" 
    xmlns:Controls1="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit" 
    xmlns:Media="clr-namespace:Microsoft.Expression.Interactivity.Media;assembly=Microsoft.Expression.Interactions" 
    xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
    xmlns:Controls2="clr-namespace:OI.Controls" mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    
    <Grid x:Name="LayoutRoot">
       
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="6" />
        </Grid.ColumnDefinitions>
        
        <ItemsControl x:Name="Items" Grid.Column="0">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <RadioButton x:Name="selRB" GroupName="LegendItems"
                                 Visibility="{Binding IsLegendItemVisible}"
                                 IsChecked="{Binding IsSelected, Mode=TwoWay}">

                        <!--UP/DOWN MENU-->
                        <Controls:ContextMenuService.ContextMenu>
                            <Controls:ContextMenu Micro:Action.TargetWithoutContext="MashupLegendViewModel"
                                                  Micro:Message.Attach="[Event Loaded] = [Action ItemMenuLoaded($source, $dataContext)]">
                                
                                <!--MOVE LAYER UP-->
                                <Controls:MenuItem Micro:Message.Attach="[Event Click] = [Action MoveLayerUp($dataContext)]">
                                    <Controls:MenuItem.Icon>
                                        <Path Data="M 4,16 12,16 12,8 16,8 8,0 0,8 4,8 4,16"  
                                                              VerticalAlignment="Center" HorizontalAlignment="Center"
                                                              Stroke="#40000000" StrokeThickness="0.75" 
                                                              StrokeEndLineCap="Round" Fill="Transparent"
                                                              StrokeLineJoin="Round"/>
                                    </Controls:MenuItem.Icon>

                                    <Controls:MenuItem.Header>
                                        <TextBlock Text="Move Up" Style="{StaticResource MenuItems}" />
                                    </Controls:MenuItem.Header>
                                </Controls:MenuItem>
                                
                                <!--MOVE LAYER DOWN-->
                                <Controls:MenuItem Micro:Message.Attach="[Event Click] = [Action MoveLayerDown($dataContext)]">
                                    <Controls:MenuItem.Icon>
                                        <Path Data="M 4,0 12,0 12,8 16,8 8,16 0,8 4,8 4,0"  
                                                              VerticalAlignment="Center" HorizontalAlignment="Center"
                                                              Stroke="#40000000" StrokeThickness="0.75" 
                                                              StrokeEndLineCap="Round" Fill="Transparent"
                                                              StrokeLineJoin="Round"/>
                                    </Controls:MenuItem.Icon>

                                    <Controls:MenuItem.Header>
                                        <TextBlock Text="Move Down" Style="{StaticResource MenuItems}" />
                                    </Controls:MenuItem.Header>
                                </Controls:MenuItem>
                                
                                <Controls:Separator/>

                                <!--REMOVE LAYER MENU ITEM-->
                                <Controls:MenuItem Micro:Message.Attach="[Event Click] = [Action RemoveLayer($dataContext)]">
                                    <Controls:MenuItem.Icon>
                                        <TextBlock Text="X" Foreground="Red" FontSize="16" HorizontalAlignment="Center" />
                                    </Controls:MenuItem.Icon>

                                    <Controls:MenuItem.Header>
                                        <TextBlock Text="Remove" Style="{StaticResource MenuItems}" />
                                    </Controls:MenuItem.Header>
                                </Controls:MenuItem>

                            </Controls:ContextMenu>
                        </Controls:ContextMenuService.ContextMenu>

                        <RadioButton.Template>
                            <ControlTemplate>
                                <Grid Background="Transparent">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    
                                    <!--VISUAL STATES FOR ITEM SELECTION-->
                                    <VisualStateManager.VisualStateGroups>
                                        <VisualStateGroup x:Name="CheckStates">
                                            <VisualState x:Name="Checked">
                                                <Storyboard>
                                                    <DoubleAnimation Storyboard.TargetName="SelectionVisualRect" 
                                                         Storyboard.TargetProperty="(UIElement.Opacity)" 
                                                         Duration="0:0:0.5" To="0.1"/>
                                                </Storyboard>
                                            </VisualState>
                                            <VisualState x:Name="Unchecked">
                                                <Storyboard>
                                                    <DoubleAnimation Storyboard.TargetName="SelectionVisualRect" 
                                                         Storyboard.TargetProperty="(UIElement.Opacity)" 
                                                         Duration="0:0:0.5" To="0"/>
                                                </Storyboard>
                                            </VisualState>
                                        </VisualStateGroup>
                                    </VisualStateManager.VisualStateGroups>

                                    <!--Selection Rectangle-->
                                    <Rectangle x:Name="SelectionVisualRect" Grid.Row="0" Opacity="0"
                                               VerticalAlignment="Top" HorizontalAlignment="Left" 
                                               Stretch="Fill" Width="{StaticResource MashupPanelSize}" Height="32"
                                               Fill="{StaticResource ThemeColorBrush}" 
                                               IsHitTestVisible="False"/>

                                    <!--Item-->
                                    <StackPanel Orientation="Horizontal" Grid.Row="0" Margin="2"  VerticalAlignment="Top" HorizontalAlignment="Left">

                                        <!--Visibility checkbox-->
                                        <CheckBox Margin="0,4,0,0" IsChecked="{Binding IsVisible, Mode=TwoWay}" IsTabStop="False"
                                                  VerticalAlignment="Top" Height="{Binding IconSize}" IsEnabled="{Binding InRange}"/>

                                        <!--Non-Subclassed Item gets a single icon-->
                                        <Image Margin="6,0,0,0" Source="{Binding Icon}" VerticalAlignment="Top" Height="{Binding IconSize}" 
                                               Visibility="{Binding IsIconVisible}"/>

                                        <!--Not Visible-->
                                        <Image Source="../Images/not_visible.png" Margin="6,0,0,0" VerticalAlignment="Top" Height="{Binding IconSize}"
                                               Visibility="{Binding IsOutOfScaleVisible}" ToolTipService.ToolTip="Layer is out of scale range"/>

                                        <!--Subclassed Item is an expander of subitems-->
                                        <Controls1:Expander Margin="6,0,0,0" ExpandDirection="Down" IsExpanded="{Binding IsSubclassExpanded, Mode=TwoWay}" IsTabStop="False"
                                            HorizontalAlignment="Center" VerticalAlignment="Top" 
                                            Background="Transparent" Style="{StaticResource OIExpanderStyle}"
                                            Visibility="{Binding IsExpanderVisible}">

                                            <Controls1:Expander.Resources>
                                                <Storyboard x:Key="CloseD">
                                                    <DoubleAnimation Storyboard.TargetName="SubclassGrid" 
                                                                     Storyboard.TargetProperty="Height" 
                                                                     Duration="0:0:0.25" 
                                                                     To="0">
                                                    </DoubleAnimation>
                                                </Storyboard>
                                                <Storyboard x:Key="OpenD">
                                                    <DoubleAnimation Storyboard.TargetName="SubclassGrid" 
                                                                     Storyboard.TargetProperty="Height" 
                                                                     Duration="0:0:0.5" 
                                                                     To="{Binding ClassesHeight}">
                                                        <DoubleAnimation.EasingFunction>
                                                            <BackEase EasingMode="EaseOut"
										                         Amplitude="0.5" />
                                                        </DoubleAnimation.EasingFunction>
                                                    </DoubleAnimation>
                                                </Storyboard>
                                            </Controls1:Expander.Resources>

                                            <i:Interaction.Triggers>
                                                <i:EventTrigger EventName="Expanded">
                                                    <Media:ControlStoryboardAction Storyboard="{StaticResource OpenD}"/>
                                                </i:EventTrigger>
                                                <i:EventTrigger EventName="Collapsed">
                                                    <Media:ControlStoryboardAction Storyboard="{StaticResource CloseD}"/>
                                                </i:EventTrigger>
                                            </i:Interaction.Triggers>

                                        </Controls1:Expander>
                                        
                                        <!--TEXT INFORMATION-->
                                        <StackPanel Orientation="Vertical" Margin="6,2,0,0">
                                            <!--Header-->
                                            <TextBlock Text="{Binding Label}"
                                                       ToolTipService.ToolTip="{Binding ToolTip}"
                                                       Style="{StaticResource LegendItems}" />

                                            <!--Filter Caption-->
                                            <TextBlock  Margin="0,-2,0,0" Visibility="{Binding HasAnyFilters, Converter={StaticResource BoolToVisibility}}"
                                                        Text="{Binding FilterCaption}" 
                                                        ToolTipService.ToolTip="{Binding FilterTooltip}" 
                                                        Style="{StaticResource LegendCaptions}" />
                                        </StackPanel>

                                    </StackPanel>

                                    <!--ICONS-->
                                    <StackPanel Orientation="Horizontal" Grid.Row="0" Margin="0,7,3,0" 
                                                VerticalAlignment="Top" HorizontalAlignment="Right"
                                                ToolTipService.ToolTip="right-click for options">

                                        <!--Density Map-->
                                        <Grid  Margin="0,0,8,0" VerticalAlignment="Center">
                                            <Controls2:DensityMapIcon
                                                Visibility="{Binding HasDensityMapSupport, Converter={StaticResource BoolToVisibility}}">
                                            </Controls2:DensityMapIcon>

                                            <Rectangle Width="16" Height="16" Stroke="LightGreen" StrokeThickness="1.5" 
                                                       Visibility="{Binding IsDensityMapVisible, Converter={StaticResource BoolToVisibility}}"/>
                                        </Grid>

                                        <!--Filter-->
                                        <Controls2:FilterIcon VerticalAlignment="Center"
                                            Margin="0,0,8,0"
                                            Visibility="{Binding HasActiveFilters, Converter={StaticResource BoolToVisibility}}">
                                        </Controls2:FilterIcon>

                                    </StackPanel>

                                    <!--Subclass Grid-->
                                    <Grid x:Name="SubclassGrid" Grid.Row="1" Height="0" Margin="22,2,0,0">
                                        <ItemsControl x:Name="SubItems"
                                                      Micro:Message.Attach="[Event Loaded] = [Action SubclassLoaded($source, $dataContext)]"
                                                      ItemsSource="{Binding Classes}">
                                            <ItemsControl.ItemTemplate>
                                                <DataTemplate>
                                                    <StackPanel Orientation="Horizontal">
                                                        <Image Margin="6,0,0,0" Source="{Binding Icon}" VerticalAlignment="Top" Height="{Binding IconSize}" />
                                                        <TextBlock Margin="6,2,0,0" Height="{Binding IconSize}" Text="{Binding Label}" Style="{StaticResource LegendItems}" />
                                                    </StackPanel>
                                                </DataTemplate>
                                            </ItemsControl.ItemTemplate>
                                        </ItemsControl>
                                    </Grid>
                                    
                                </Grid>
                            </ControlTemplate>
                        </RadioButton.Template>
                    </RadioButton>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

        <!--SEP-->
        <Grid Grid.Column="1" Background="#08000000" Margin="4,4,0,4"/>
    </Grid>
</UserControl>
